package com.cloudsigns.factorymanager2.produce.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.customview.ListViewForScrollView;
import com.cloudsigns.factorymanager2.produce.adapter.ProduceProcessInforPlanAdapter;
import com.cloudsigns.factorymanager2.produce.bean.Produce_ProductBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartBean;
import com.cloudsigns.factorymanager2.produce.iview.IProductOrderEditView;
import com.cloudsigns.factorymanager2.produce.presenter.ProductOrderEditPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;


public class ProduceWorkpartInformationActivity1 extends BaseActivity implements IProductOrderEditView {
    private String TAG = ProduceWorkpartInformationActivity1.class.getSimpleName();
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_workpart_name;
    private ImageView img_date, img_edit;
    private LinearLayout ll_content, ll_cycle;
    private TextView edt_specifition_size, edt_slip_size, edt_process_num, edt_cycle_date;
    private ListViewForScrollView lv_cycle_num, lv_rework_cycle;
    private TextView btn_submit, btn_delete;
    private WorkPartBean workPartBean;
    private int position = 0;

    private ProduceProcessInforPlanAdapter planAdapter;
    private ProductOrderEditPresenter productOrderEditPresenter;

    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_produce_workpart_information1);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        tv_workpart_name = findViewById(R.id.tv_workpart_name);
        img_date = findViewById(R.id.img_date);
        img_edit = findViewById(R.id.img_edit);
        ll_content = findViewById(R.id.ll_content);
        ll_cycle = findViewById(R.id.ll_cycle);
        edt_specifition_size = findViewById(R.id.edt_specifition_size);
        edt_slip_size = findViewById(R.id.edt_slip_size);
        edt_process_num = findViewById(R.id.edt_process_num);
        edt_cycle_date = findViewById(R.id.edt_cycle_date);
        lv_cycle_num = findViewById(R.id.lv_cycle_num);
        lv_rework_cycle = findViewById(R.id.lv_rework_cycle);
        btn_submit = findViewById(R.id.btn_submit);
        btn_delete = findViewById(R.id.btn_delete);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText(getResources().getString(R.string.order_information));

        Intent intent = getIntent();
        if (null != intent) {
            workPartBean = (WorkPartBean) intent.getSerializableExtra(ConstantUtil.KEY_1);
            position = intent.getIntExtra(ConstantUtil.KEY_2, 0);
        }

        planAdapter = new ProduceProcessInforPlanAdapter(this);
        planAdapter.setData(workPartBean.getWorkPartScheduleList());
        lv_cycle_num.setAdapter(planAdapter);

        CommonEnum.ProduceOrderStatus status = workPartBean.getStatus();
        if (null != status) {
            if (status == CommonEnum.ProduceOrderStatus.RECEIVED || status == CommonEnum.ProduceOrderStatus.RETURNED || status == CommonEnum.ProduceOrderStatus.COMPLETED) {
                ll_content.setVisibility(View.VISIBLE);
                ll_cycle.setVisibility(View.VISIBLE);
                btn_submit.setBackgroundResource(R.mipmap.btn_long_bg_gray);
                btn_submit.setEnabled(false);
            } else {
                ll_content.setVisibility(View.GONE);
                ll_cycle.setVisibility(View.GONE);
                btn_submit.setBackgroundResource(R.drawable.btn_green_submit);
            }
        }

        CommonEnum.ProductShapeType shape = workPartBean.getShapeType().getType();
        if (null != shape) {
            if (shape == CommonEnum.ProductShapeType.ROUND) {
                edt_specifition_size.setText("直径" + workPartBean.getShapeType().getDiameter() + "mm*" + "厚度" + workPartBean.getShapeType().getThick() + "mm");
            } else if (shape == CommonEnum.ProductShapeType.RECTANGLE) {
                edt_specifition_size.setText("长度" + workPartBean.getShapeType().getWidth() + "mm*" + "高度" + workPartBean.getShapeType().getHeight() + "mm*" + "厚度" + workPartBean.getShapeType().getThick() + "mm");
            } else if (shape == CommonEnum.ProductShapeType.SPECIALSHAPE) {
                edt_specifition_size.setText(workPartBean.getShapeType().getDesc());
            }
        }

        edt_slip_size.setText("上" + workPartBean.getShapeType().getBorderTop() + "mm*" + "下" + workPartBean.getShapeType().getBorderBottom() + "mm*" + "左" + workPartBean.getShapeType().getBorderLeft() + "mm*" + "右" + workPartBean.getShapeType().getBorderRight() + "mm");
        tv_workpart_name.setText(workPartBean.getWorkPartName());
        edt_process_num.setText(workPartBean.getAmount() + workPartBean.getUnit());
        edt_cycle_date.setText(DateUtil.getYyyy_Mm_Dd(workPartBean.getStartDate()) + "—" + DateUtil.getYyyy_Mm_Dd(workPartBean.getCompleteDate()));

        productOrderEditPresenter = new ProductOrderEditPresenter(this);
    }

    @Override
    public void setListener() {
        rl_back.setOnClickListener(this);
        img_date.setOnClickListener(this);
        img_edit.setOnClickListener(this);
        btn_submit.setOnClickListener(this);
        btn_delete.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View view) {
        Intent mIntent = null;
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            // 编辑加工计划
            case R.id.img_date:
                if (!TextUtils.isEmpty(DateUtil.getYyyy_Mm_Dd(workPartBean.getStartDate())) && !TextUtils.isEmpty(DateUtil.getYyyy_Mm_Dd(workPartBean.getCompleteDate()))) {
                    mIntent = new Intent(ProduceWorkpartInformationActivity1.this, ProduceEditProcessPlanActivity.class);
                    mIntent.putExtra(ConstantUtil.KEY_1, workPartBean);
                    startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_3);
                } else {
                    showToast(getResources().getString(R.string.edit_process_information_pl));
                }
                break;
            // 编辑加工信息
            case R.id.img_edit:
                mIntent = new Intent(ProduceWorkpartInformationActivity1.this, ProduceEditProcessInformationActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, workPartBean);
                startActivityForResult(mIntent, ConstantUtil.ADD_WORKPART_INFOMATION);
                break;
            // 确认编辑
            case R.id.btn_submit:
                if (!workPartBean.isEdit()) {
                    showToast("请编辑加工信息");
                    return;
                } else if (ll_cycle.getVisibility() == View.GONE) {
                    showToast("请编辑加工计划");
                    return;
                } else {
                    productOrderEditPresenter.productOrderEdit();
                }
                break;
            // 删除
            case R.id.btn_delete:
                LayoutInflater inflater = getLayoutInflater();
                View view1 = inflater.inflate(R.layout.dialog_delete, null);
                Button btn_cancle = view1.findViewById(R.id.btn_cancel);
                Button btn_sub = view1.findViewById(R.id.btn_submit);
                TextView tv_content = view1.findViewById(R.id.tv_content);
                final Dialog dialog = new Dialog(ProduceWorkpartInformationActivity1.this);
                Context context = dialog.getContext();
                try {
                    int divierId = context.getResources().getIdentifier("android:id/titleDivider", null, null);
                    View divider = dialog.findViewById(divierId);
                    divider.setBackgroundColor(Color.TRANSPARENT);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                dialog.show();
                Window window = dialog.getWindow();
                WindowManager.LayoutParams lp = window.getAttributes();
                window.setBackgroundDrawable(new ColorDrawable(0));
                window.setGravity(Gravity.CENTER);
                window.setAttributes(lp);
                dialog.setContentView(view1);
                tv_content.setText("确定删除该条编辑信息吗？");

                // 取消
                btn_cancle.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                });

                // 确定
                btn_sub.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                        if (!TextUtils.isEmpty(workPartBean.getWorkPartId())) {
                            // todo 调接口
                        } else {
                            workPartBean.resetDefault();
                            Intent mIntent = new Intent();
                            mIntent.putExtra(ConstantUtil.KEY_1, workPartBean);
                            setResult(ConstantUtil.START_ACTIVITY_KEY_3, mIntent);
                            showToast(getResources().getString(R.string.delete_success));
                            finish();
                        }

                    }
                });
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            // 编辑加工信息
            case ConstantUtil.ADD_WORKPART_INFOMATION:
                if (data != null) {
                    WorkPartBean wpBean = (WorkPartBean) data.getSerializableExtra(ConstantUtil.WORKPARTBEAN);

                    if (null != wpBean) {
                        ll_content.setVisibility(View.VISIBLE);

                        workPartBean = wpBean;

                        CommonEnum.ProductShapeType shape = wpBean.getShapeType().getType();
                        if (null != shape) {

                            if (shape == CommonEnum.ProductShapeType.ROUND) {
                                edt_specifition_size.setText("直径" + wpBean.getShapeType().getDiameter() + "mm*" + "厚度" + wpBean.getShapeType().getThick() + "mm");
                            } else if (shape == CommonEnum.ProductShapeType.RECTANGLE) {
                                edt_specifition_size.setText("长度" + wpBean.getShapeType().getWidth() + "mm*" + "高度" + wpBean.getShapeType().getHeight() + "mm*" + "厚度" + wpBean.getShapeType().getThick() + "mm");
                            } else if (shape == CommonEnum.ProductShapeType.SPECIALSHAPE) {
                                edt_specifition_size.setText(wpBean.getShapeType().getDesc());
                            }
                        }
                        edt_slip_size.setText("上" + wpBean.getShapeType().getBorderTop() + "mm*" + "下" + wpBean.getShapeType().getBorderBottom() + "mm*" + "左" + workPartBean.getShapeType().getBorderLeft() + "mm*" + "右" + workPartBean.getShapeType().getBorderRight() + "mm");
                        tv_workpart_name.setText(wpBean.getWorkPartName());
                        edt_process_num.setText(wpBean.getAmount() + wpBean.getUnit());
                        edt_cycle_date.setText(DateUtil.getYyyy_Mm_Dd(wpBean.getStartDate()) + "—" + DateUtil.getYyyy_Mm_Dd(wpBean.getCompleteDate()));

                    }
                }
                break;
            // 编辑加工计划
            case ConstantUtil.START_ACTIVITY_KEY_3:
                if (data != null) {
                    ll_cycle.setVisibility(View.VISIBLE);
                    WorkPartBean wpBean = (WorkPartBean) data.getSerializableExtra(ConstantUtil.KEY_1);
                    workPartBean = wpBean;
                    planAdapter = new ProduceProcessInforPlanAdapter(this);
                    planAdapter.setData(wpBean.getWorkPartScheduleList());
                    lv_cycle_num.setAdapter(planAdapter);
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public long getBeginDate() {
        return workPartBean.getStartDate();
    }

    @Override
    public long getFinishDate() {
        return workPartBean.getCompleteDate();
    }

    @Override
    public String getOrderId() {
        return workPartBean.getOrderId();
    }

    @Override
    public String getProductId() {
        return workPartBean.getProductId();
    }


    @Override
    public List<WorkPartBean> getWorkPartList() {
        List<WorkPartBean> workPartBeanList = new ArrayList<WorkPartBean>();
        workPartBeanList.add(workPartBean);
        return workPartBeanList;
    }

    @Override
    public void getProductOrderEditSuccessed() {
        showToast(getResources().getString(R.string.edit_success));
        Intent mIntent = new Intent();
        mIntent.putExtra(ConstantUtil.KEY_1, workPartBean);
        setResult(ConstantUtil.START_ACTIVITY_KEY_3, mIntent);
        finish();
    }

    @Override
    public void getProductOrderEditFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
